#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <gtk/gtk.h>
#define MAXSIZE 100

extern GtkTextBuffer* rcv_buffer;

void FileRecv()
{
    int frecv_sockfd, acc_sockfd;
    struct sockaddr_in frecv_address;
    struct sockaddr_in fremote_address;
    int res;
    int len;
    int i;
    int k;
    int j;
    int fsize;
    int fp;
    char buffer;
    char fn[MAXSIZE];
    char recvfn[MAXSIZE] = "/home/tarena/Test";
    //GtkTextIter end;
   // GtkTextIter start;

    //设置sockaddr_in中的参数 
    frecv_address.sin_family = AF_INET;
    frecv_address.sin_addr.s_addr = htonl(INADDR_ANY);
    frecv_address.sin_port = htons(5050);

    //创建socket
    if ((frecv_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("socket");
        exit(EXIT_FAILURE);
    }

    //允许重复使用IP地址与套接字进行绑定
    i = 1;
    setsockopt(frecv_sockfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));

    //绑定socket与IP地址，接受方相当于服务器
    if (bind(frecv_sockfd, (struct sockaddr*)&frecv_address,
        sizeof(struct sockaddr)) < 0)
    {
        perror("bind");
        exit(EXIT_FAILURE);
    }

    //启动监听
    if (listen(frecv_sockfd, 5) == -1)
    {
        perror("listen");
        exit(EXIT_FAILURE);
    }

    //接受客户端的连接，开始传输文件
    len = sizeof(fremote_address);
    if ((acc_sockfd = accept(frecv_sockfd, (struct sockaddr*)&fremote_address, &len)) == -1)
    {
        perror("accept");
        exit(EXIT_FAILURE);
    }

    //接收文件名 
    if ((res = recv(acc_sockfd, fn, MAXSIZE, 0)) < 0)
    {
        perror("sendto");
        exit(EXIT_FAILURE);
    }

    //指定文件名
    for (i = 0; i < strlen(fn); i++)
        if (fn[i] == '/') k = i;
    j = 0;
    for (i = k; i <= strlen(fn); i++)
        fn[j++] = fn[i];
    strcat(recvfn, fn);

    for (i = 0; i < strlen(recvfn); i++)
	{

		printf("%c",recvfn[i]);
	}
	printf("\n");
       
    //在当前目录下创建文件并关闭
    if ((fp = creat(recvfn, S_IRUSR | S_IWUSR)) == -1)
    {
        perror("creat file");
        exit(1);
    }
    close(fp);

    //打开文件
    if ((fp = open(recvfn, O_RDWR)) == -1)
    {
        perror("recv file open");
        exit(1);
    }

    //接收文件长度
    if ((res = recv(acc_sockfd, &fsize, 2, 0)) < 0)
    {
        perror("sendto");
        exit(EXIT_FAILURE);
    }

    //将文件存储在原有文件名的文件中；（位置当前目录下）
    i = 0;
    while (i < fsize)
    {
        if ((res = recv(acc_sockfd, &buffer, 1, 0)) < 0)
        {
            perror("recvfrom");
            break;
        }
        write(fp, &buffer, 1);
        i++;
    }

	printf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");

    /*
//对gtk界面的操作 
    gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(rcv_buffer), &start, &end);

    //判断文件接收正确与否
    if (i == fsize)
        gtk_text_buffer_insert(GTK_TEXT_BUFFER(rcv_buffer), &end,
            "********Receive successful!**********\n", -1);
    else
        gtk_text_buffer_insert(GTK_TEXT_BUFFER(rcv_buffer), &end,
            "********Receive failed!**********\n", -1);

    //输出接收方文件名  
    gtk_text_buffer_insert(GTK_TEXT_BUFFER(rcv_buffer), &end,
        recvfn, strlen(recvfn));
    gtk_text_buffer_insert(GTK_TEXT_BUFFER(rcv_buffer), &end, "\n", 1);
*/

    //关闭文件与套接口
    close(fp);
    close(frecv_sockfd);
}
